import type { DeptDetailResult } from "@/api/types";
import { defineStore } from "pinia";
import { ref } from "vue";
import { userDeptStorage } from "@/config/storage";

/** 当前用户部门信息 */
export const useUserDeptStore = defineStore("userDept", () => {
    /** 部门ID */
    const id = ref(userDeptStorage.value?.id || "");
    /** 上级部门ID */
    const parentId = ref(userDeptStorage.value?.parentId || null);
    /** 部门类型 */
    const typeName = ref(userDeptStorage.value?.typeName || null);
    /** 部门名称 */
    const name = ref(userDeptStorage.value?.name || "");
    /** 联系电话 */
    const phone = ref(userDeptStorage.value?.phone || null);
    /** 联系邮箱 */
    const email = ref(userDeptStorage.value?.email || null);
    /** 部门层级 */
    const level = ref(userDeptStorage.value?.level || 0);
    /** 部门编码 */
    const code = ref(userDeptStorage.value?.code || "");
    /** 备注 */
    const remark = ref(userDeptStorage.value?.remark || null);

    function SET_DEPT(dept: DeptDetailResult): void {
        id.value = dept.id;
        parentId.value = dept.parentId;
        typeName.value = dept.typeName;
        name.value = dept.name;
        phone.value = dept.phone || null;
        email.value = dept.email || null;
        level.value = dept.level;
        code.value = dept.code;
        remark.value = dept.remark || null;
    }

    return {
        id,
        name,
        phone,
        email,
        level,
        code,
        remark,
        parentId,
        typeName,
        SET_DEPT,
    };
});
